package com.meida.zsd.app.user.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.springmvc.base.BaseController;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import com.meida.module.weixin.provider.config.WxMpProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description: 微信相关接口
 * @ClassName WebChatController
 * @date: 2022.06.24 09:30
 * @Author: ldd
 */
@RestController
@RequestMapping("/common/webchat/")
@Api(tags = "微信相关接口")
@Log4j2
public class WebChatController extends BaseController<AppUserService, AppUser> {

    @Autowired
    WxMpProperties wxMpProperties;


    @GetMapping("getOpenId")
    @ApiOperation(value = "获取用户openid", notes = "openid", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", required = true)
    })
    public ResultBody getOpenId(@RequestParam("code") String code) {
        try {
            String appId = wxMpProperties.getConfigs().get(0).getAppId();
            String secret = wxMpProperties.getConfigs().get(0).getSecret();
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code".replace("APPID", appId).replace("SECRET", secret).replace("CODE", code);
            String result = HttpUtil.get(url);
            log.info(result);
            return ResultBody.ok(JSON.parseObject(result).getString("openid"));

        } catch (Exception e) {
            log.error(e);
        }
        return ResultBody.failed("获取用户失败");

    }

    @Resource
    private AppUserService appUserService;

    @GetMapping("init")
    @ApiOperation(value = "获取用户openid", notes = "openid", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", required = true)
    })
    public ResultBody getOpenId1() {
        try {
            for (int i = 0; i < 100; i++) {

                String accountName = String.valueOf((System.currentTimeMillis() / 1000) + Integer.parseInt(IdWorker.getIdStr().substring(12)));
                String password = "123456";
                String shareCode = "";
                String areaCode = "86";
                String userType = "MOBILE";
                appUserService.registerByMobile(accountName, password, userType, shareCode, areaCode);
            }

        } catch (Exception e) {
            log.error(e);
        }
        return ResultBody.failed("获取用户失败");

    }


}
